<template>
    <div>
        <div class="container">
            <div class="header">
                <div class="query-item">
                    <span class="query-title">状态：</span>
                    <el-select
                        class="query-select"
                        v-model="query.status"
                        size="mini"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="待审核" value="0"></el-option>
                        <el-option label="已通过" value="1"></el-option>
                        <el-option label="已拒绝" value="2"></el-option>
                    </el-select>
                </div>
                <div class="query-item">
                    <span class="query-title">类目：</span>
                    <el-cascader
                        class="query-select"
                        v-model="categoryIdList"
                        :options="categoryList"
                        style="width: 120px"
                        size="mini"
                        :show-all-levels="false"
                        @change="queryCategoryChange"
                        clearable
                    ></el-cascader>
                </div>
                <div class="query-item">
                    <span class="query-title">专区：</span>
                    <el-select
                        class="query-select"
                        v-model="query.showgoods"
                        size="mini"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </div>
                <div class="query-item">
                    <span class="query-title">分销：</span>
                    <el-select
                        class="query-select"
                        v-model="query.distri"
                        size="mini"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </div>
                <div class="query-item">
                    <span class="query-title">在售：</span>
                    <el-select
                        class="query-select"
                        v-model="query.inonsale"
                        size="mini"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </div>
                <div class="query-item">
                    <span class="query-title">推荐：</span>
                    <el-select
                        class="query-select"
                        v-model="query.ishot"
                        size="mini"
                        placeholder="请选择"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="是" value="0"></el-option>
                        <el-option label="否" value="1"></el-option>
                    </el-select>
                </div>
                <div class="query-item">
                    <el-input
                        class="query-input"
                        v-model="query.brandname"
                        clearable
                        size="mini"
                        placeholder="商家名称"
                    ></el-input>
                    <el-input
                        class="query-input"
                        v-model="query.goodsname"
                        clearable
                        size="mini"
                        placeholder="商品名称"
                    ></el-input>
                    <el-button type="primary" size="mini" @click="getData(true)">查询</el-button>
                    <el-button type="success" size="mini" @click="add">新增</el-button>
                </div>
            </div>

            <el-table
                :data="list"
                style="width: 100%"
                v-loading="loading"
                @sort-change="sortChange"
            >
                <el-table-column prop="goodsId" label="商品ID"></el-table-column>
                <el-table-column label="所属商家" width="200">
                    <template #default="scope">
                        <p class="text-ellipsis-2">{{scope.row.brandname}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="商品名称" width="200">
                    <template #default="scope">
                        <p class="text-ellipsis-2">{{scope.row.goodsname}}</p>
                    </template>
                </el-table-column>
                <el-table-column prop="catename" label="类目"></el-table-column>
                <el-table-column label="商品图" width="90">
                    <template #default="scope">
                        <el-image
                            style="width: 80px; height: 80px"
                            :src="scope.row.goodspic"
                            :preview-src-list="[scope.row.goodspic]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="原价">
                    <template #default="scope">
                        <span>¥{{scope.row.counprice.toFixed(2)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="单独购买价" width="120">
                    <template #default="scope">
                        <span>¥{{scope.row.retprice.toFixed(2)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="拼团优惠">
                    <template #default="scope">
                        <span>¥{{scope.row.discount.toFixed(2)}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="browse" label="浏览量" sortable="custom"></el-table-column>
                <el-table-column prop="sales" label="销量" sortable="custom"></el-table-column>
                <el-table-column prop="addtime" label="添加时间" width="100"></el-table-column>
                <el-table-column label="状态">
                    <template #default="scope">
                        <el-tag v-if="scope.row.status === 0" size="mini">待审核</el-tag>
                        <el-tag v-if="scope.row.status === 1" size="mini" type="success">已通过</el-tag>
                        <el-tag v-if="scope.row.status === 2" size="mini" type="danger">已拒绝</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="专区" width="60">
                    <template #default="scope">
                        <el-switch
                            :value="scope.row.isshow"
                            inactive-color="#ff4949"
                            @change="updateGoodStateStatus($event, 1, scope.row.goodsId, scope.row.grouponRulesId)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="分销" width="60">
                    <template #default="scope">
                        <el-switch
                            :value="scope.row.isdistri"
                            inactive-color="#ff4949"
                            @change="updateGoodStateStatus($event, 2, scope.row.goodsId, scope.row.grouponRulesId)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="在售" width="60">
                    <template #default="scope">
                        <el-switch
                            :value="scope.row.isonsale"
                            inactive-color="#ff4949"
                            @change="updateGoodStateStatus($event, 3, scope.row.goodsId, scope.row.grouponRulesId)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="推荐" width="60">
                    <template #default="scope">
                        <el-switch
                            :value="scope.row.ishot"
                            inactive-color="#ff4949"
                            @change="updateGoodStateStatus($event, 4, scope.row.goodsId, scope.row.grouponRulesId)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="140">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.status === 1"
                            type="primary"
                            size="mini"
                            @click="edit(scope.row)"
                        >编辑</el-button>
                        <el-popconfirm
                            v-if="scope.row.status === 0"
                            confirmButtonText="通过"
                            cancelButtonText="拒绝"
                            cancelButtonType="danger"
                            @confirm="updateGoodsStatus(scope.row.goodsId,true)"
                            @cancel="updateGoodsStatus(scope.row.goodsId,false)"
                            title="审核"
                        >
                            <el-button slot="reference" type="primary" size="mini">审核</el-button>
                        </el-popconfirm>

                        <!-- 删除 -->
                        <el-popconfirm title="确认删除该商品?" @confirm="deleteGoods(scope.row.goodsId)">
                            <el-button
                                slot="reference"
                                type="danger"
                                size="mini"
                                icon="el-icon-delete"
                                style="margin-left: 10px"
                            ></el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>

            <div v-if="list.length" class="pagination">
                <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="query.page"
                    :page-size="query.size"
                    layout="total, prev, pager, next"
                    :total="query.total"
                ></el-pagination>
            </div>
        </div>

        <el-dialog :title="type ? '编辑商品' : '添加商品'" :visible.sync="dialogVisible" width="950px">
            <div class="dialog-inner">
                <el-form label-width="120px" size="small">
                    <!-- <el-divider>基本信息</el-divider> -->
                    <el-form-item label="商品名称:">
                        <el-input v-model="form.name" maxlength="20"></el-input>
                    </el-form-item>
                    <el-form-item label="商品简介:">
                        <el-input v-model="form.brief" type="textarea" maxlength="200"></el-input>
                    </el-form-item>
                    <el-row :gutter="0">
                        <el-col :span="9">
                            <el-form-item label="原价:">
                                <el-input v-model="form.counterPrice" class="inline-cell"></el-input>元
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item label="单独购买价:">
                                <el-input v-model="form.retailPrice" class="inline-cell"></el-input>元
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="0">
                        <el-col :span="9">
                            <el-form-item label="所属分类:">
                                <el-cascader
                                    v-model="form.categoryId"
                                    :options="categoryList"
                                    style="width: 160px"
                                ></el-cascader>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item label="所属商家:">
                                <el-select v-model="form.brandId" filterable class="inline-cell">
                                    <el-option
                                        v-for="i in brandList"
                                        :key="i.id"
                                        :label="i.name"
                                        :value="i.id"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="库存:">
                        <el-input v-model="form.number" class="inline-cell"></el-input>
                    </el-form-item>
                    <el-form-item label="封面图:">
                        <ImgUploader v-model="form.picUrl" />
                    </el-form-item>
                    <el-form-item label="商品主图:">
                        <MultImgUploader v-model="form.gallery" :maxNum="9" />
                    </el-form-item>
                    <el-form-item label="商品详情图:">
                        <MultImgUploader v-model="form.detail" :maxNum="9" />
                    </el-form-item>

                    <el-row :gutter="0">
                        <el-col :span="9">
                            <el-form-item label="成团人数:">
                                <el-input v-model="form.discount_member" class="inline-cell"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="9">
                            <el-form-item label="拼团优惠:">
                                <el-input v-model="form.discount" class="inline-cell"></el-input>元
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="状态:">
                        <el-select v-model="form.status">
                            <el-option label="待审核" :value="0"></el-option>
                            <el-option label="通过" :value="1"></el-option>
                            <el-option label="拒绝" :value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label>
                        <el-button @click="postData" type="primary">提 交</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>

        <!-- <el-dialog title v-model="auditDialogVisible" width="750px" @closed="resetForm">
        <div class="dialog-inner">
            <el-form label-width="100px">
                <el-form-item label="审核:">
                    <el-radio v-model="audit" :label="1">通过</el-radio>
                    <el-radio v-model="audit" :label="2">拒绝</el-radio>
                </el-form-item>
                <el-form-item v-if="audit === 2" label="原因:">
                    <el-input v-model="form.remarks" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label>
                    <el-button @click="auditDialogVisible = false" size="mini">取消</el-button>
                    <el-button @click="auditDo" type="primary" size="mini">确定</el-button>
                </el-form-item>
            </el-form>
        </div>
        </el-dialog>-->
    </div>
</template>

<script>
import { getCategoryList, getBrandList } from '@/api/common'
import * as request from '@/api/goods'
import ImgUploader from '@/components/ImgUploader'
import MultImgUploader from '@/components/MultImgUploader'
export default {
    components: {
        ImgUploader,
        MultImgUploader
    },
    data() {
        return {
            categoryIdList: '',
            query: {
                status: '',
                categoryId: '',
                showgoods: '',
                distri: '',
                inonsale: '',
                ishot: '',
                brandname: '',
                goodsname: '',
                browse: '',
                sales: '',
                page: 1,
                size: 10,
                total: 0
            },
            type: 0,
            categoryList: [],
            brandList: [],
            list: [],
            loading: false,
            dialogVisible: false,
            auditDialogVisible: false,
            audit: '',
            form: {
                goodsId: '',
                grouponRulesId: '',
                status: 1,
                goodSn: '',
                name: '',
                counterPrice: '',
                retailPrice: '',
                categoryId: '',
                brief: '',
                number: '',
                brandId: '',
                picUrl: '',
                gallery: [],
                detail: [],
                discount: '',
                discount_member: '',
            },
        }
    },
    mounted() {
        this.getData()
        this.getCategoryList()
        this.getbrandList()
    },
    methods: {
        async getbrandList() {
            try {
                const { errno, data } = await getBrandList()
                if (errno === 0) {
                    this.brandList = data
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.error(err)
            }
        },
        queryCategoryChange(val) {
            this.query.categoryId = val ? val[1] : ''
        },
        async getCategoryList() {
            try {
                const { errno, data } = await getCategoryList()
                if (errno === 0) {
                    const parentList = data.categoryList.filter(i => data.allList[i.id].length)
                    const categoryList = parentList.map(item => {
                        const childList = data.allList[item.id].map(i => {
                            return {
                                label: i.name,
                                value: i.id
                            }
                        })
                        return {
                            label: item.name,
                            value: item.id,
                            children: childList
                        }
                    })
                    this.categoryList = categoryList
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.error(err)
            }
        },
        handleCurrentChange(val) {
            this.query.page = val
            this.getData()
        },

        sortChange(e) {
            if (e.prop && e.prop === "browse" && e.order === 'ascending') {
                this.query.browse = "asc"
                this.query.sales = ""
            } else if (e.prop && e.prop === "browse" && e.order === 'descending') {
                this.query.browse = "desc"
                this.query.sales = ""
            } else if (e.prop && e.prop === "sales" && e.order === 'ascending') {
                this.query.browse = ""
                this.query.sales = "asc"
            } else if (e.prop && e.prop === "sales" && e.order === 'descending') {
                this.query.browse = ""
                this.query.sales = "desc"
            } else {
                this.query.browse = ""
                this.query.sales = ""
            }
            this.getData()
        },

        /* 获取列表数据 */

        async getData(isSearch) {
            if (isSearch) {
                this.query.page = 1
            }
            this.loading = true
            try {
                const { errno, data, errmsg } = await request.getList(this.query)
                if (errno === 0) {
                    this.list = data.list || []
                    this.query.page = data.page
                    this.query.total = data.total
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.error(err)
            }
            this.loading = false
        },

        resetForm() {
            this.audit = ''
            this.form.id = ''
            this.form.picture = ''
            this.form.audit = ''
            this.form.status = ''
            this.form.remarks = ''
        },

        add() {
            this.type = 0
            this.form.goodsId = ''
            this.form.grouponRulesId = ''
            this.form.status = 1
            this.form.goodSn = ''
            this.form.name = ''
            this.form.counterPrice = ''
            this.form.retailPrice = ''
            this.form.categoryId = ''
            this.form.brief = ''
            this.form.number = ''
            this.form.brandId = ''
            this.form.picUrl = ''
            this.form.gallery = []
            this.form.detail = []
            this.form.discount = ''
            this.form.discount_member = ''
            this.dialogVisible = true
        },

        edit(item) {
            this.type = 1
            this.form.goodsId = item.goodsId
            this.form.grouponRulesId = item.grouponRulesId
            this.form.status = item.status
            this.form.name = item.goodsname
            this.form.counterPrice = item.counprice
            this.form.retailPrice = item.retprice
            this.form.brief = item.brief
            this.form.number = item.number
            this.form.brandId = item.brandId
            this.form.picUrl = item.goodspic || ''
            this.form.gallery = item.gallery || []
            this.form.detail = item.detail || []
            this.form.discount = item.discount
            this.form.discount_member = item.discountMember
            this.form.categoryId = []
            this.categoryList.forEach(p => {
                p.children.forEach(c => {
                    if (c.value === item.categoryId) {
                        this.form.categoryId.push(p.value)
                        this.form.categoryId.push(c.value)
                    }
                })
            })
            this.dialogVisible = true
        },

        // async auditDo() {
        //     if (!this.audit) {
        //         return this.$message.warning('请选择审核')
        //     }
        //     let params = {
        //         id: this.form.id
        //     }
        //     if (this.audit === 1) {
        //         params.status = this.form.status + 1
        //     } else {
        //         params.status = 4
        //         params.remarks = this.form.remarks
        //     }
        //     try {
        //         const { errno, errmsg } = await request.updateAppwith(params, 'post')
        //         if (errno === 0) {
        //             this.$message.success(errmsg)
        //             this.auditDialogVisible = false
        //             this.getData()
        //         } else {
        //             this.$message.warning(errmsg)
        //         }
        //     } catch (err) {
        //         console.log(err)
        //     }
        // },

        async updateGoodsStatus(id, flag) {
            const params = {
                id,
                status: flag ? 1 : 2,
                msg: ''
            }
            try {
                const { errno, errmsg } = await request.updateGoodsStatus(params)
                if (errno === 0) {
                    this.$message.success(errmsg)
                    this.getData()
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.log(err)
            }
        },

        async updateGoodStateStatus(status, type, id, grouponRulesId) {
            const params = { id, type, status, grouponRulesId }
            try {
                const { errno, errmsg } = await request.updateGoodStateStatus(params)
                if (errno === 0) {
                    this.$message.success(errmsg)
                    this.getData()
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.log(err)
            }
        },

        async postData() {
            if (!this.form.name) {
                return this.$message.warning('请输入商品名称')
            }
            let params = { ...this.form }
            params.categoryId = params.categoryId[1]
            if (this.type === 0) {
                delete params.goodsId
                delete params.grouponRulesId
            }
            try {
                const { errno, errmsg } = await request[this.type ? 'updateGoods' : 'insertGoods'](params, 'post')
                if (errno === 0) {
                    this.$message.success(errmsg)
                    this.dialogVisible = false
                    this.getData()
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.log(err)
            }
        },
        async deleteGoods(goodsId) {
            try {
                const { errno, errmsg } = await request.deleteGoods({ goodsId })
                if (errno === 0) {
                    this.$message.success(errmsg)
                    this.getData()
                } else {
                    this.$message.warning(errmsg)
                }
            } catch (err) {
                console.error(err)
            }
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    padding: 20px;
}
.header {
    margin-bottom: 30px;
    .query-item {
        display: inline-block;
        margin-right: 20px;
        .query-title {
            font-size: 13px;
        }
        .query-select {
            width: 80px;
        }
        .query-input {
            width: 120px;
            margin-right: 10px;
        }
    }
}
.inline-cell {
    width: 160px;
    margin-right: 8px;
}
.noData {
    margin-top: 40px;
    text-align: center;
    color: #888;
}
.dialog-inner {
    margin-top: -20px;
    padding: 0 10px;
}
.pagination {
    margin: 25px 0;
    text-align: center;
}
</style>
